<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
      v-show="showSearch"
    >
      <el-row>
        <el-form-item label="出库日期" style="width: 350px">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="纸张名称" prop="name">
          <el-input
            v-model.trim="queryParams.name"
            style="width: 160px"
            placeholder="请输入纸张名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <!-- <el-form-item label="领用机台" prop="machineName">
                    <el-input
                        v-model.trim="queryParams.machineName"
                        style="width: 180px"
                        placeholder="请输入领用机台"
                        @keyup.enter="handleQuery"
                    />
                </el-form-item> -->
        <el-form-item label="定量" prop="weight">
          <el-input
            type="number"
            v-model.trim="queryParams.weight"
            style="width: 120px"
            placeholder="请输入定量"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="幅宽" prop="longWide">
          <el-input
            v-model.trim="queryParams.longWide"
            clearable
            style="width: 120px"
            placeholder="请输入幅宽"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          <el-button @click="advanced = !advanced">
            <a style="color: #448ef7">
              <el-icon v-if="advanced">
                <ArrowUpBold />
              </el-icon>
              <el-icon v-else>
                <ArrowDownBold />
              </el-icon>
            </a>
          </el-button>
          <right-toolbar
            style="margin-left: 10px"
            :search="false"
            v-model:showSearch="showSearch"
            @queryTable="getList"
            :columns="columns"
          ></right-toolbar>
        </el-form-item>
        <el-row v-if="advanced">
          <el-form-item label="纸张编码" prop="code">
            <el-input
              v-model.trim="queryParams.code"
              style="width: 270px"
              placeholder="请输入纸张编码"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item label="纸张品牌" prop="brand">
            <el-select
              v-model="queryParams.brand"
              clearable
              filterable
              placeholder="请选择纸张品牌"
              style="width: 150px"
            >
              <el-option
                v-for="item in mes_paper_brand"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="规格型号" prop="specs">
            <el-select
              v-model="queryParams.specs"
              clearable
              placeholder="选择规格型号"
              style="width: 150px"
            >
              <el-option
                v-for="item in mes_paper_specs"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="厂/客料" prop="material">
            <el-select
              v-model="queryParams.material"
              clearable
              placeholder="选择厂/客料"
              style="width: 160px"
            >
              <el-option
                v-for="item in mes_paper_material"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="单位" prop="unit">
            <el-select
              v-model="queryParams.unit"
              clearable
              placeholder="选择单位"
              style="width: 180px"
            >
              <el-option
                v-for="item in mes_paper_unit"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-row>
        <!-- <div class="operation">
                    <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                    <el-button icon="Refresh" @click="resetQuery">重置</el-button>
                    <el-button @click="advanced = !advanced">
                        <a style="color: #448ef7">
                            <el-icon v-if="advanced">
                                <ArrowUpBold />
                            </el-icon>
                            <el-icon v-else>
                                <ArrowDownBold />
                            </el-icon>
                        </a>
                    </el-button>
                    <right-toolbar
                        style="margin-left: 10px"
                        :search="false"
                        v-model:showSearch="showSearch"
                        @queryTable="getList"
                        :columns="columns"
                    ></right-toolbar>
                </div> -->
      </el-row>
      <el-row v-if="advanced"></el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
                <el-button type="primary" plain icon="Download" @click="handleExport">导出</el-button>
            </el-col> -->
    </el-row>
    <div>
      <el-table
        v-loading="loading"
        border
        :data="dataList"
        :summary-method="getSummaries"
        show-summary
      >
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          v-if="columns[2].visible"
          label="出库单编码"
          min-width="200"
          align="center"
          prop="outboundCode"
          :show-overflow-tooltip="true"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleViewOutbound(row)" text>
              {{ row.outboundCode }}
            </el-button>
          </template>
        </el-table-column>
        <!-- <el-table-column
                    v-if="columns[3].visible"
                    label="机台名称"
                    min-width="150"
                    align="center"
                    prop="machineName"
                    :show-overflow-tooltip="true"
                /> -->
        <el-table-column
          v-if="columns[4].visible"
          label="出库日期"
          min-width="150"
          align="center"
          prop="outboundDate"
        >
          <template v-slot="{ row }">
            {{ row.outboundDate ? row.outboundDate.split("T")[0] : "" }}
          </template>
        </el-table-column>
        <el-table-column
          label="纸型"
          v-if="columns[5].visible"
          align="center"
          prop="paperType"
        >
          <template #default="scope">
            <dict-tag :options="mes_paper_type" :value="scope.row.paperType" />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[6].visible"
          label="纸张编码"
          min-width="150"
          align="center"
          prop="code"
        >
          <template v-slot="{ row }">
            <el-button type="primary" @click="handleViewPaperCode(row)" text>
              {{ row.code }}
            </el-button>
          </template>
        </el-table-column>
        <!-- <el-table-column v-if="columns[7].visible" label="自有编码" min-width="120" align="center" prop="originalCode"
                                 :show-overflow-tooltip="true" /> -->
        <el-table-column
          v-if="columns[8].visible"
          label="纸张名称"
          min-width="160"
          align="center"
          prop="name"
        />
        <el-table-column
          v-if="columns[9].visible"
          label="规格"
          min-width="120"
          align="center"
          prop="specs"
        >
          <template #default="scope">
            <span v-if="scope.row.longWide">
              {{ scope.row.longWide }}
            </span>
            <span v-if="scope.row.specs">
              <dict-tag :options="mes_paper_specs" :value="scope.row.specs" />
            </span>
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[10].visible"
          min-width="120"
          label="品牌"
          align="center"
          prop="brand"
        >
          <template #default="scope">
            <dict-tag :options="mes_paper_brand" :value="scope.row.brand" />
          </template>
        </el-table-column>
        <!-- <el-table-column
                    v-if="columns[11].visible"
                    width="80"
                    label="幅宽"
                    align="center"
                    prop="longWide"
                /> -->
        <el-table-column
          v-if="columns[11].visible"
          width="80"
          label="定量"
          align="center"
          prop="weight"
        />
        <el-table-column
          v-if="columns[2].visible"
          label="厂/客料"
          min-width="120"
          align="center"
          prop="material"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <dict-tag
              :options="mes_paper_material"
              :value="scope.row.material"
            />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[12].visible"
          width="80"
          label="单位"
          align="center"
          prop="unit"
        >
          <template #default="scope">
            <dict-tag :options="mes_paper_unit" :value="scope.row.unit" />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[13].visible"
          min-width="100"
          fixed="right"
          label="出库数量"
          align="center"
          prop="outboundNumber"
        />
        <el-table-column
          v-if="columns[14].visible"
          label="备注"
          align="center"
          prop="remark"
          :show-overflow-tooltip="true"
        />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup name="order">
import { getCuttingOutboundDetailList } from "@/api/storage/paperOutBound.js";
import { readonly } from "vue";

const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const router = useRouter();
const total = ref(0);
const advanced = ref(false);
const outboundNumberSum = ref(0);

// 列显隐信息
const columns = ref([
  { key: 0, label: `出库单编码`, visible: true },
  { key: 1, label: `生产单编码`, visible: true },
  { key: 2, label: `领用机台编码`, visible: true },
  { key: 3, label: `机台名称`, visible: true },
  { key: 4, label: `出库日期`, visible: true },
  { key: 5, label: `纸型`, visible: true },
  { key: 6, label: `纸张编码`, visible: true },
  { key: 7, label: `自有编码`, visible: true },
  { key: 8, label: `纸张名称`, visible: true },
  { key: 9, label: `规格型号`, visible: true },
  { key: 10, label: `品牌`, visible: true },
  { key: 11, label: `定量`, visible: true },
  { key: 12, label: `单位`, visible: true },
  { key: 13, label: `出库数量`, visible: true },
  { key: 14, label: `备注`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 30,
    warehousingCode: undefined,
    supplierName: undefined,
    deliveryMan: undefined,
    warehousingDate: undefined,
  },
});
// 合计
const getSummaries = (param) => {
  const { columns, data } = param;
  const sums = [];
  columns.forEach((column, index) => {
    if (index === 0) {
      sums[index] = "合计";
      return;
    }
    switch (column.property) {
      case "outboundNumber":
        sums[index] = outboundNumberSum.value;
        break;
      default:
        break;
    }
  });
  return sums;
};
const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  return firstDay;
};
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);
const { queryParams } = toRefs(data);
const {
  mes_paper_type,
  mes_paper_specs,
  mes_paper_brand,
  mes_paper_unit,
  mes_paper_material,
} = proxy.useDict(
  "mes_paper_type",
  "mes_paper_specs",
  "mes_paper_brand",
  "mes_paper_unit",
  "mes_paper_material"
);

const getList = () => {
  loading.value = true;
  queryParams.value.startDate = Array.isArray(dateRange.value)
    ? dateRange.value[0]
    : undefined;
  queryParams.value.endDate =
    Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined;
  getCuttingOutboundDetailList(queryParams.value)
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      if (res.rows.length > 0) {
        res.rows[0].outboundNumberSum = 0;
        res.rows.forEach((row) => {
          res.rows[0].outboundNumberSum =
            res.rows[0].outboundNumberSum + Number(row.outboundNumber || 0);
        });
        outboundNumberSum.value = res.rows[0].outboundNumberSum;
      }
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};

// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  proxy.resetForm("queryRef");
  handleQuery();
};

const handleViewOutbound = (row) => {
  router.push({
    path: "/storage/paperOutbound/outboundDetails",
    query: {
      code: row.outboundCode,
      id: row.id,
      info: true,
      readonly: true,
    },
  });
};
// 查看纸张
const handleViewPaperCode = (row) => {
  router.push({
    path: "/basicInfo/paperCoding/details",
    query: {
      code: row.code,
      id: row.paperId,
      readonly: true,
    },
  });
};
// // 查看机台
// const handleViewMachineName = (row) => {
//     router.push({
//         path: '/basicInfo/workbench/details',
//         query: {
//             code: row.machineNo,
//         }
//     })
// }
// 查看生产单
const handleViewOrderNo = (row) => {
  if (row.produceNo.indexOf("FM") > -1) {
    router.push({
      path: "/production/cover/coverDetails",
      query: {
        id: row.produceId,
        info: true,
        readonly: true,
        moduleCode: "searchAccess",
      },
    });
  } else {
    router.push({
      path: "/production/text/textDetails",
      query: {
        id: row.produceId,
        info: true,
        readonly: true,
        moduleCode: "searchAccess",
      },
    });
  }
};

getList();
// router.beforeEach((to, from, next) => {
//     if (to.path == "/basicInfo/personnel") {
//         getList()
//     }
//     next();
// });
</script>

<style lang="scss" scoped>
.operation {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 18px;
}
</style>
